package com.example.usermanagement.controller;

import com.example.usermanagement.dto.request.UserAddRequest;
import com.example.usermanagement.dto.request.UserQueryRequest;
import com.example.usermanagement.dto.request.UserUpdateRequest;
import com.example.usermanagement.dto.response.CommonResponse;
import com.example.usermanagement.dto.response.PageResponse;
import com.example.usermanagement.dto.response.UserResponse;
import com.example.usermanagement.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户管理相关接口")
public class SysUserController {


    @Resource
    private SysUserService sysUserService;

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询用户", description = "根据用户ID查询用户详细信息")
    public CommonResponse<UserResponse> getUserById(
            @Parameter(description = "用户ID", required = true) @PathVariable Long id) {
        UserResponse userResponse = sysUserService.getUserById(id);
        return CommonResponse.success(userResponse);
    }

    @GetMapping("/list")
    @Operation(summary = "查询用户列表", description = "查询用户列表，支持分页和条件查询")
    public CommonResponse<PageResponse<UserResponse>> getUserList(UserQueryRequest queryRequest) {
        PageResponse<UserResponse> pageResponse = sysUserService.getUserList(queryRequest);
        return CommonResponse.success(pageResponse);
    }

    @PostMapping
    @Operation(summary = "新增用户", description = "创建新用户")
    public CommonResponse<Long> addUser(@Validated @RequestBody UserAddRequest userAddRequest) {
        Long userId = sysUserService.addUser(userAddRequest);
        return CommonResponse.success(userId);
    }

    @PutMapping
    @Operation(summary = "更新用户", description = "更新用户信息")
    public CommonResponse<Boolean> updateUser(@Validated @RequestBody UserUpdateRequest userUpdateRequest) {
        boolean success = sysUserService.updateUser(userUpdateRequest);
        return CommonResponse.success(success);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户", description = "根据ID删除用户")
    public CommonResponse<Boolean> deleteUser(
            @Parameter(description = "用户ID", required = true) @PathVariable Long id) {
        boolean success = sysUserService.deleteUser(id);
        return CommonResponse.success(success);
    }
}